package com.banmao.algorithm.No201To300.No203;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution1 {
    public ListNode removeElements(ListNode head, int val) {

        // 遍历链表呗
        // 定义一个虚拟头节点
        ListNode dummy_head = new ListNode();
        dummy_head.next = head;

        ListNode preNode = dummy_head;
        ListNode currentNode = dummy_head.next;


        while (currentNode != null) {
            if(currentNode.val == val) {
                // 将上一个节点的指针指向后面一个
                preNode.next = currentNode.next;
            } else {
                preNode = preNode.next;
            }
            currentNode = currentNode.next;
        }

        return dummy_head.next;
    }
}
